package com.huatai.verity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.util.ObjectUtil;
import com.huatai.common.util.StringUtil;
import com.huatai.verity.entity.AppService;
import com.huatai.verity.mapper.AppServiceMapper;
import com.huatai.verity.service.AppServiceService;
import com.huatai.verity.vo.AppServiceParmVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @program: ht-system
 * @description: 服务应用接口
 * @author: songxiude
 * @create: 2023-07-21 11:48
 **/
@Service
@Slf4j
public class AppServiceServiceImpl extends ServiceImpl<AppServiceMapper, AppService> implements AppServiceService {

	/**
	 * 新增修改应用服务
	 *
	 * @param appService
	 * @return
	 */
	public boolean saveUpdate(AppService appService) {
		//如果id不传视为新增
		if (ObjectUtil.isEmpty(appService.getId()) || appService.getId().longValue() <= 0) {
			appService.setId(IdWorker.getId());
		}
		boolean saveOrUpdate = saveOrUpdate(appService);
		return saveOrUpdate;
	}

	/**
	 * 获取应用服务列表
	 *
	 * @param appServiceParmVo
	 * @return
	 */
	public IPage<AppService> getAppServiceList(AppServiceParmVo appServiceParmVo) {
		long p = appServiceParmVo.getPage() > 0 ? appServiceParmVo.getPage() : 1;
		long size = appServiceParmVo.getSize() > 0 ? appServiceParmVo.getSize() : 10;
		Page<AppService> page = Page.of(p, size);
		LambdaQueryWrapper<AppService> queryWrapper = new LambdaQueryWrapper<>();
		//如果传入服务名称模糊查询
		queryWrapper.like(StringUtil.isNotBlank(appServiceParmVo.getServiceName()), AppService::getServiceName, appServiceParmVo.getServiceName());
		//如果传入部署方式模糊查询
		queryWrapper.like(StringUtil.isNotBlank(appServiceParmVo.getDepWay()), AppService::getDepWay, appServiceParmVo.getDepWay());
		queryWrapper.orderByDesc(AppService::getCreateTime);
		return baseMapper.selectPage(page, queryWrapper);
	}
}
